package com.kws.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.domain.TbExtend;
import com.kws.system.domain.info.QueryExtendInfo;
import com.kws.system.domain.vo.CreateExtendRq;
import com.kws.system.domain.vo.QueryExtendRq;
import com.kws.system.domain.vo.UpdateExtendRq;
import com.kws.system.mapper.ExtendMapper;
import com.kws.system.service.ExtendImageService;
import com.kws.system.service.ExtendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <b>ExtendServiceImpl</b>
 *
 * @description: ExtendServiceImpl <br>
 * @date: 2024/12/3 17:28 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class ExtendServiceImpl extends ServiceImpl<ExtendMapper, TbExtend> implements ExtendService {
    private static final Logger logger = LoggerFactory.getLogger(ExtendServiceImpl.class);

    @Autowired
    private ExtendImageService extendImageService;


    @Override
    public Page<QueryExtendInfo> queryExtendPage(QueryExtendRq rq) {
        Page<QueryExtendInfo> page = new Page<>(rq.getPageNum(), rq.getPageSize());
        return baseMapper.queryExtendPage(page,rq);
    }

    @Override
    public boolean createExtend(CreateExtendRq rq) {
        TbExtend tbExtend = new TbExtend();
        BeanUtil.copyProperties(rq,tbExtend);
        Ksys.setBasicModelData(tbExtend, SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
        int insert = baseMapper.insert(tbExtend);
        if (1 == insert){
            extendImageService.createExtendImage(tbExtend.getId(),rq.getFiles());
            return true;
        }
        return false;
    }

    @Override
    public QueryExtendInfo queryExtendById(Long id) {
        return baseMapper.queryExtendById(id);
    }

    @Override
    @Transactional
    public boolean updateExtendById(UpdateExtendRq rq) {
        TbExtend tbExtend = baseMapper.selectById(rq.getId());
        tbExtend.setModuleType(rq.getModuleType());
        tbExtend.setCommodityName(rq.getCommodityName());
        tbExtend.setPictureLink(rq.getPictureLink());
        tbExtend.setImageSort(rq.getImageSort());
        Ksys.setBasicModelData(tbExtend,SecurityUtils.getStringUserId(),KConstants.DATA_ENABLE,ModelOpType.UPDATE);
        int i = baseMapper.updateById(tbExtend);
        if (1 == i){
            extendImageService.updateExtendImage(rq);
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteExtendById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        updateWrapper.set("update_user",SecurityUtils.getStringUserId());
        updateWrapper.set("update_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }
}
